﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class 序列反射動態程式集_反射Namespace下所有類_成員_的名字 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = 得到一個namespace下的所有类的名字();
        GridView1.DataBind();

        得到Type類中所有的屬性();
    }

    #region 如何获得某个namespace下的所有类的名字？ http://topic.csdn.net/t/20061126/13/5185960.html

    public System.Collections.IList 得到一個namespace下的所有类的名字()
    {

        var 當前執行的的命名空間的所有類的名字 = System.Reflection.Assembly.GetExecutingAssembly().GetTypes();

        /// <summary>
        /// 將對像類型不能做為資料源的資料通過類型轉換,轉換在資料源
        /// </summary>
        /// <param name="TypeCollection">從程式集裏得到的所有類的類型
        /// </param>
        //轉換類型到一個強類型的資料源中(當前執行的的命名空間的所有類的名字);


        return 轉換類型到一個強類型的資料源中(當前執行的的命名空間的所有類的名字);

    }
    /// <summary>
    /// 將對像類型不能做為資料源的資料通過類型轉換,轉換在資料源
    /// 返回 Ilist  的資料源
    /// </summary>
    /// <param name="TypeCollection">從程式集裏得到的所有類的類型
    /// </param>
    protected System.Collections.IList 轉換類型到一個強類型的資料源中(Type[] TypeCollection)
    {

        //一個新的對像集合
        List<TypeClass> typeClasses = new List<TypeClass>();

        foreach (var d in TypeCollection)
        {


           TypeClass typeClassobj =    new TypeClass();

            //遍歷 類裏的屬性關把值傳過去
            foreach (System.Reflection.PropertyInfo PropertyInfoObj in d.GetType().GetProperties())
            {
                //開始比較傳屬性的值

                #region 傳值

                typeClassobj.Module = d.Module;
                typeClassobj.Assembly = d.Assembly;
                typeClassobj.TypeHandle = d.TypeHandle;
               // typeClassobj.DeclaringMethod = d.DeclaringMethod;
                typeClassobj.BaseType = d.BaseType;
                typeClassobj.UnderlyingSystemType = d.UnderlyingSystemType;
                typeClassobj.FullName = d.FullName;
                typeClassobj.AssemblyQualifiedName = d.AssemblyQualifiedName;
                typeClassobj.Namespace = d.Namespace;
                typeClassobj.GUID = d.GUID;
                //typeClassobj.GenericParameterAttributes = d.GenericParameterAttributes;
                typeClassobj.IsGenericTypeDefinition = d.IsGenericTypeDefinition;
                typeClassobj.IsGenericParameter = d.IsGenericParameter;
               // typeClassobj.GenericParameterPosition = d.GenericParameterPosition;
                typeClassobj.IsGenericType = d.IsGenericType;
                typeClassobj.ContainsGenericParameters = d.ContainsGenericParameters;
                typeClassobj.StructLayoutAttribute = d.StructLayoutAttribute;
                typeClassobj.Name = d.Name;
                typeClassobj.MemberType = d.MemberType;
                typeClassobj.DeclaringType = d.DeclaringType;
                typeClassobj.ReflectedType = d.ReflectedType;
                typeClassobj.MetadataToken = d.MetadataToken;
                typeClassobj.TypeInitializer = d.TypeInitializer;
                typeClassobj.IsNested = d.IsNested;
                typeClassobj.Attributes = d.Attributes;
                typeClassobj.IsVisible = d.IsVisible;
                typeClassobj.IsNotPublic = d.IsNotPublic;
                typeClassobj.IsPublic = d.IsPublic;
                typeClassobj.IsNestedPublic = d.IsNestedPublic;
                typeClassobj.IsNestedPrivate = d.IsNestedPrivate;
                typeClassobj.IsNestedFamily = d.IsNestedFamily;
                typeClassobj.IsNestedAssembly = d.IsNestedAssembly;
                typeClassobj.IsNestedFamANDAssem = d.IsNestedFamANDAssem;
                typeClassobj.IsNestedFamORAssem = d.IsNestedFamORAssem;
                typeClassobj.IsAutoLayout = d.IsAutoLayout;
                typeClassobj.IsLayoutSequential = d.IsLayoutSequential;
                typeClassobj.IsExplicitLayout = d.IsExplicitLayout;
                typeClassobj.IsClass = d.IsClass;
                typeClassobj.IsInterface = d.IsInterface;
                typeClassobj.IsValueType = d.IsValueType;
                typeClassobj.IsAbstract = d.IsAbstract;
                typeClassobj.IsSealed = d.IsSealed;
                typeClassobj.IsEnum = d.IsEnum;
                typeClassobj.IsSpecialName = d.IsSpecialName;
                typeClassobj.IsImport = d.IsImport;
                typeClassobj.IsSerializable = d.IsSerializable;
                typeClassobj.IsAnsiClass = d.IsAnsiClass;
                typeClassobj.IsUnicodeClass = d.IsUnicodeClass;
                typeClassobj.IsAutoClass = d.IsAutoClass;
                typeClassobj.IsArray = d.IsArray;
                typeClassobj.IsByRef = d.IsByRef;
                typeClassobj.IsPointer = d.IsPointer;
                typeClassobj.IsPrimitive = d.IsPrimitive;
                typeClassobj.IsCOMObject = d.IsCOMObject;
                typeClassobj.HasElementType = d.HasElementType;
                typeClassobj.IsContextful = d.IsContextful;
                typeClassobj.IsMarshalByRef = d.IsMarshalByRef; 

                #endregion

                

            }

            typeClasses.Add(typeClassobj);
        }

        return typeClasses;

    }


    protected void 得到Type類中所有的屬性()
    { 
        
        foreach(System.Reflection.PropertyInfo d in typeof(Type).GetType().GetProperties())
        {
            //Response.Write("public " + d.PropertyType+ " " + d.Name + " { get; set; } \r\n");

            //輸出所有類型傳值
            Response.Write(" typeClassobj.  " + d.Name + " =d." + d.Name + "; \r\n");


        }


    }




    

    #endregion



    #region 定義一個類型轉換的強類型

    /// <summary>
    /// 定義一個類型轉換的強類型
    /// 用於轉換 Type不能做為資料源的問題
    /// </summary>
    public class TypeClass {

        public System.Reflection.Module Module { get; set; }
        public System.Reflection.Assembly Assembly { get; set; }
        public System.RuntimeTypeHandle TypeHandle { get; set; }
        public System.Reflection.MethodBase DeclaringMethod { get; set; }
        public System.Type BaseType { get; set; }
        public System.Type UnderlyingSystemType { get; set; }
        public System.String FullName { get; set; }
        public System.String AssemblyQualifiedName { get; set; }
        public System.String Namespace { get; set; }
        public System.Guid GUID { get; set; }
        public System.Reflection.GenericParameterAttributes GenericParameterAttributes { get; set; }
        public System.Boolean IsGenericTypeDefinition { get; set; }
        public System.Boolean IsGenericParameter { get; set; }
        public System.Int32 GenericParameterPosition { get; set; }
        public System.Boolean IsGenericType { get; set; }
        public System.Boolean ContainsGenericParameters { get; set; }
        public System.Runtime.InteropServices.StructLayoutAttribute StructLayoutAttribute { get; set; }
        public System.String Name { get; set; }
        public System.Reflection.MemberTypes MemberType { get; set; }
        public System.Type DeclaringType { get; set; }
        public System.Type ReflectedType { get; set; }
        public System.Int32 MetadataToken { get; set; }
        public System.Reflection.ConstructorInfo TypeInitializer { get; set; }
        public System.Boolean IsNested { get; set; }
        public System.Reflection.TypeAttributes Attributes { get; set; }
        public System.Boolean IsVisible { get; set; }
        public System.Boolean IsNotPublic { get; set; }
        public System.Boolean IsPublic { get; set; }
        public System.Boolean IsNestedPublic { get; set; }
        public System.Boolean IsNestedPrivate { get; set; }
        public System.Boolean IsNestedFamily { get; set; }
        public System.Boolean IsNestedAssembly { get; set; }
        public System.Boolean IsNestedFamANDAssem { get; set; }
        public System.Boolean IsNestedFamORAssem { get; set; }
        public System.Boolean IsAutoLayout { get; set; }
        public System.Boolean IsLayoutSequential { get; set; }
        public System.Boolean IsExplicitLayout { get; set; }
        public System.Boolean IsClass { get; set; }
        public System.Boolean IsInterface { get; set; }
        public System.Boolean IsValueType { get; set; }
        public System.Boolean IsAbstract { get; set; }
        public System.Boolean IsSealed { get; set; }
        public System.Boolean IsEnum { get; set; }
        public System.Boolean IsSpecialName { get; set; }
        public System.Boolean IsImport { get; set; }
        public System.Boolean IsSerializable { get; set; }
        public System.Boolean IsAnsiClass { get; set; }
        public System.Boolean IsUnicodeClass { get; set; }
        public System.Boolean IsAutoClass { get; set; }
        public System.Boolean IsArray { get; set; }
        public System.Boolean IsByRef { get; set; }
        public System.Boolean IsPointer { get; set; }
        public System.Boolean IsPrimitive { get; set; }
        public System.Boolean IsCOMObject { get; set; }
        public System.Boolean HasElementType { get; set; }
        public System.Boolean IsContextful { get; set; }
        public System.Boolean IsMarshalByRef { get; set; } 
    }
    #endregion
}
